import { nativeMenuData } from "./menu";
import * as Types from "./types";
import { Dispatch } from "redux";
import { MyAxios, IResBody } from "../config/http";

export interface IAction {
  type: string;
  data?: any;
}

// export const getMenuAct = () => ({
//   type: Types.GET_MENU,
//   // data: resData
//   data: nativeMenuData
// });

// 先注释,这是从后端获取菜单,为了测试方便先不从后端获取
export const getMenuAct = () => {
  return (dispatch: Dispatch) => {
    dispatch({ type: Types.SET_LOADING_MENU, data: true });
    MyAxios.axios
      .get("user/web/menu", {
        headers: { ...MyAxios.getToken() }
      })
      .then(res => {
        const resData: IResBody = res.data;
        const action: IAction = {
          type: Types.GET_MENU,
          data: resData
        };
        dispatch(action);
      })
      .catch(e => {})
      .finally(() => {
        dispatch({ type: Types.SET_LOADING_MENU, data: false });
      });
  };
};

// 修改登录弹框的visible属性
export const setLoginModalVisibleAct = (visible: boolean) => {
  return { type: Types.SET_LOGIN_MODEL_VISIBLE, data: visible };
};

// 修改左侧菜单的收起/展开
export function setCollapsedAct(data: boolean): IAction {
  return {
    type: Types.SET_COLLAPSED,
    data
  };
}

// 获取当前用户信息
export function getUserInfoAct() {
  return (dispatch: Dispatch) => {
    dispatch({ type: Types.SET_LOADING_USER_INFO, data: true });
    MyAxios.axios
      .get("user/info", {
        headers: { ...MyAxios.getToken() }
      })
      .then(res => {
        const resData: IResBody = res.data;
        const action: IAction = {
          type: Types.SET_USER_INFO,
          data: resData
        };
        dispatch(action);
      })
      .catch(e => {})
      .finally(() => {
        dispatch({ type: Types.SET_LOADING_USER_INFO, data: false });
      });
  };
}
